"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var forms_1 = require("@angular/forms");
function getStrLength(str) {
    if (!str) {
        str = '';
    }
    var len = str.length ? str.length : 0;
    var reLen = 0;
    for (var i = 0; i < len; i++) {
        if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
            // Full width
            reLen += 2;
        }
        else {
            // Half width
            reLen++;
        }
    }
    return reLen;
}
exports.getStrLength = getStrLength;
function charLengthValidator(maxLength) {
    return function (control) {
        var str = control.value;
        var reLen = getStrLength(str);
        return reLen > maxLength ? { 'validateLength': { maxLength: maxLength } } : null;
    };
}
exports.charLengthValidator = charLengthValidator;
var ValidateLengthDirective = /** @class */ (function () {
    function ValidateLengthDirective() {
        this.valFn = forms_1.Validators.nullValidator;
    }
    ValidateLengthDirective_1 = ValidateLengthDirective;
    ValidateLengthDirective.prototype.ngOnChanges = function (changes) {
        var change = changes['appValidateLength'];
        if (change) {
            var val = change.currentValue;
            this.valFn = charLengthValidator(val);
        }
        else {
            this.valFn = forms_1.Validators.nullValidator;
        }
    };
    ValidateLengthDirective.prototype.validate = function (control) {
        return this.valFn(control);
    };
    __decorate([
        core_1.Input()
    ], ValidateLengthDirective.prototype, "appValidateLength", void 0);
    ValidateLengthDirective = ValidateLengthDirective_1 = __decorate([
        core_1.Directive({
            selector: '[appValidateLength]',
            providers: [{ provide: forms_1.NG_VALIDATORS, useExisting: ValidateLengthDirective_1, multi: true }]
        })
    ], ValidateLengthDirective);
    return ValidateLengthDirective;
    var ValidateLengthDirective_1;
}());
exports.ValidateLengthDirective = ValidateLengthDirective;
